
const { $Toast } = require('../../dist/base/index');
const tusi=0;
Page({
  clickMenu(e) {
    switch (e.detail.index) {
      case 1:
        this.tusi = -1;
        this.copyBook(e.currentTarget.dataset.bookid);
        break;
      case 2:
        this.deleteBook(e);
        break;
      case 0:
        wx.navigateTo({
          url: '/pages/addBook/addBook?bookid=' + e.currentTarget.dataset.bookid
        })
        break;
    }
  },
selectAll(){
  if (this.tusi == 0){
    wx.showLoading({
      title: '加载数据中'
    });
  }
 

  getApp().sendRequest({
    url: "getBooks",
    method: "GET",
    success: res => {
      if (res.data.code == 200) {
        for (var index in res.data.data.gradeLst) {
          this.data.gradeName.push(res.data.data.gradeLst[index].gradename);
        }
        this.setData({
          bookLst: res.data.data.bookLst,
          gradeLst: res.data.data.gradeLst
        })
        wx.hideLoading();
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    },
    fail: res => {
      console.log("fail", res)
    },
    complete: res => {
      console.log("complete", res)
    }
  })
},
  skipSection(e){
    wx.navigateTo({
      url: '/pages/ch_zhujiahao/ch_zhujiahao?bookid='+e.currentTarget.dataset.bookid
    })
  },
  copyBook(id){
    getApp().sendRequest({
      url:"upgradeBook",
      data:{
        bookId:id
      },
      success:res=>{
        console.log("success", res);
        if (res && res.data){
          wx.showToast({
            title: res.data.msg
          })

          this.selectAll()
        }
        
      },
      fail: err => {
        wx.showToast({
          title: '服务器异常'
        })
      }

    })
  },

  deleteBook(e) {
    wx.showModal({
      title: '提示',
      content: '确认删除吗?',
      success: res => {
        if (res.confirm) {
          console.log(e)
          getApp().sendRequest({
            url: "delBook",
            method: "GET",
            data:{
              id : e.currentTarget.dataset.bookid
            },
            success: res => {
              if (res.data.code == 200) {
                this.data.bookLst.splice(e.currentTarget.dataset.index, 1);
                this.setData({
                  bookLst: this.data.bookLst
                });
                wx.showToast({
                  title: ''+res.data.msg,
                })
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none'
                })
              }
            },
            fail: res => {
              console.log("fail", res)
            },
            complete: res => {
              console.log("complete", res)
            }
          })
        }
      },
      fail: f => {
        console.log("11" + f)
      }
    })
  },

  zoom(e) {
    this.data.gradeLst[e.currentTarget.dataset.index].isBindZoom = !this.data.gradeLst[e.currentTarget.dataset.index].isBindZoom;
    this.setData({
      gradeLst: this.data.gradeLst
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    actions: [{
        name: '编辑',
        width: 100,
        color: '#80848f',
        fontsize: '20'
      },
      {
        name: '复制',
        width: 100,
        color: '#DDDDDD',
        background: '#0066FF',
        fontsize: '20'
      },
      {
        name: '删除',
        color: '#fff',
        fontsize: '20',
        width: 100,
        background: '#ed3f14'
      }
    ],
    scrollTop: 0,
    isBindZoom: false,
    bookLst:[],
    gradeName : []
  },
  onChange(event) {
    console.log(event.detail, 'click right menu callback data')
  },
  //页面滚动执行方式
  onPageScroll(event) {
    this.setData({
      scrollTop: event.scrollTop
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.selectAll();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})